using MediatR;
using Medical.Domain;
using Medical.Infrastructrue.Interfaces;
using Medical.ReadApi.Dto;
using Medical.WriteApi.Appcontext.Command;
using Medical.WriteApi.Dto;
using Yitter.IdGenerator;

namespace Medical.WriteApi.Appcontext.CommandHandler
{
	public class CreateDiseaseCommandHandler : IRequestHandler<CreateDiseaseCommand, DiseaseDto>
	{
		private readonly IBaseRepository<Disease> _repository;

		public CreateDiseaseCommandHandler(IBaseRepository<Disease> repository)
		{
			_repository = repository;
		}

		public async Task<DiseaseDto> Handle(CreateDiseaseCommand request, CancellationToken cancellationToken)
		{
			var entity = new Disease { DiseaseId = YitIdHelper.NextId(), DepartmentId = request.DepartmentId, Name = request.Name };
			await _repository.InsertAsync(entity);
			return new DiseaseDto { DiseaseId = entity.DiseaseId, DepartmentId = entity.DepartmentId, Name = entity.Name, CreatedAt = entity.CreatedAt, UpdatedAt = entity.UpdatedAt };
		}
	}
}
